﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Implement;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.DepartmentCommand;
using D.Medicalcare.Read.API.Application.Command.RegistrationCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.RegistrationHandler
{
    /// <summary>
    /// 通过医院Id获取科室Id
    /// </summary>
    public class GetDepartmentIdHandler:IRequestHandler<GetDepartmentIdCommand, APIResult<List<Department>>>
    {

        private readonly IBaseRepository<Department> repository;
    private readonly IBaseRepository<HospitalNavigation> baseRepository;

    public GetDepartmentIdHandler(IBaseRepository<Department> repository, IBaseRepository<HospitalNavigation> baseRepository)
    {
        this.repository = repository;
        this.baseRepository = baseRepository;
    }

    public async Task<APIResult<List<Department>>> Handle(GetDepartmentIdCommand request, CancellationToken cancellationToken)
    {
        try
        {
            APIResult<List<Department>> result = new APIResult<List<Department>>();
            result.Code = APIEnum.Mvc;
            result.Msg = "查询成功";
            var list = repository.GetAll();

            if (request.CampusId >0)
            {
                list = list.Where(a => a.CampusId == request.CampusId);

            }
            if (request.DeparId>0)
            {
                list = list.Where(a => a.DeparId == request.DeparId);

            }
                if (request.SuperAffiliation !=null)
                {
                    list = list.Where(a => a.SuperAffiliation == request.SuperAffiliation);

                }

                result.Code=APIEnum.Success;
            result.Msg="查询成功";
            result.Data = list.ToList();
            return await Task.FromResult(result);
        }
        catch (Exception)
        {

            throw;
        }
    }
}
}
